<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
    <title></title>

    <script type="text/javascript">
        var djConfig = { isDebug: false };
    </script>

    <script type="text/javascript" src="${pageContext.request.contextPath}/js/dojo/dojo.js.uncompressed.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/dojox/cometd.js.uncompressed.js"></script>

    <script type="text/javascript" language="JavaScript">

        dojo.require("dojox.cometd");

        $ = dojo.byId;

        function setup() {
            dojox.cometd.init("${pageContext.request.contextPath}/cometd");
            dojox.cometd.subscribe("/broadcast", "receiveMessage");
        }

        function receiveMessage(message) {
            $("serverTime").innerHTML = "The local time on the Jetty server is " + message.data;
        }

        dojo.addOnLoad(setup);        
    </script>
</head>

<body>

<h1>Broadcasting Bayeux Messages</h1>

<p>This example shows how easy it is to send messages to a BayeuxChannel. The BroadcastTask is a simple TimerTask that
    sends the local time of the server to the BayeuxChannel once every second.</p>

<blockquote>
    <div style="color:green;" id="serverTime"></div>
</blockquote>

<h3>Java Code</h3>

<pre>
    public class BroadcastTask extends TimerTask
    {
        private BayeuxChannel channel;

        @Required
        public void setChannel(BayeuxChannelImpl channel) {
            this.channel = channel;
        }

        public void run() {
            channel.publish(new Date().toString());
        }
    }
</pre>

<h3>JavaScript Code</h3>

<pre>
    function setup() {
        dojo.event.connect(cometd, "finishInit",
            function(type, data, evt, request) {
                cometd.subscribe("/broadcast", true, "receiveMessage");
            }
        );
        cometd.init({}, "/cometd");
    }

    function receiveMessage(message) {
        $("serverTime").innerHTML = "The local time on the Jetty server is " + message.data;
    }
</pre>

<h3>Spring Configuration</h3>

<pre>
    &lt;bean id="broadcastChannel" class="com.polarrose.spring.bayeux.impl.BayeuxChannelImpl">
      &lt;property name="name" value="/broadcast"/>
    &lt;/bean>

    &lt;bean id="broadcastTask" class="com.polarrose.spring.bayeux.examples.broadcast.BroadcastTask">
      &lt;property name="channel" ref="broadcastChannel"/>
    &lt;/bean>

    &lt;!-- ... configuration to schedule the broadcastTask omitted .. -->
</pre>

</body>

</html>
